package cn.iocoder.yudao.module.yyzx.controller.admin.elden;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotations.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.yyzx.controller.admin.elden.vo.*;
import cn.iocoder.yudao.module.yyzx.dal.dataobject.elden.EldenDO;
import cn.iocoder.yudao.module.yyzx.service.elden.EldenService;

@Tag(name = "管理后台 - 老头管理")
@RestController
@RequestMapping("/yyzx/elden")
@Validated
public class EldenController {

    @Resource
    private EldenService eldenService;

    @PostMapping("/create")
    @Operation(summary = "创建老头管理")
    @PreAuthorize("@ss.hasPermission('yyzx:elden:create')")
    public CommonResult<Integer> createElden(@Valid @RequestBody EldenSaveReqVO createReqVO) {
        return success(eldenService.createElden(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新老头管理")
    @PreAuthorize("@ss.hasPermission('yyzx:elden:update')")
    public CommonResult<Boolean> updateElden(@Valid @RequestBody EldenSaveReqVO updateReqVO) {
        eldenService.updateElden(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除老头管理")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('yyzx:elden:delete')")
    public CommonResult<Boolean> deleteElden(@RequestParam("id") Integer id) {
        eldenService.deleteElden(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得老头管理")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('yyzx:elden:query')")
    public CommonResult<EldenRespVO> getElden(@RequestParam("id") Integer id) {
        EldenDO elden = eldenService.getElden(id);
        return success(BeanUtils.toBean(elden, EldenRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得老头管理分页")
    @PreAuthorize("@ss.hasPermission('yyzx:elden:query')")
    public CommonResult<PageResult<EldenRespVO>> getEldenPage(@Valid EldenPageReqVO pageReqVO) {
        PageResult<EldenDO> pageResult = eldenService.getEldenPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, EldenRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出老头管理 Excel")
    @PreAuthorize("@ss.hasPermission('yyzx:elden:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportEldenExcel(@Valid EldenPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<EldenDO> list = eldenService.getEldenPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "老头管理.xls", "数据", EldenRespVO.class,
                        BeanUtils.toBean(list, EldenRespVO.class));
    }

}